<?php

namespace app\admin\dao;

use app\common\model\MemberMatchRecord;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * MemberMatchRecordDao 数据访问对象
 * Class MemberMatchRecordDao
 * @package app\admin\dao
 * @method MemberMatchRecord findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method MemberMatchRecord insertActive(array $params, array $allow_field = [])
 * @method MemberMatchRecord updateActive(MemberMatchRecord $model, array $params, array $allow_field = [])
 * @method MemberMatchRecord findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method MemberMatchRecord updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(MemberMatchRecord $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class MemberMatchRecordDao
{
    use DaoTrait;

    /**
     * @var MemberMatchRecord
     */
    private $baseModel;

    /**
     * MemberMatchRecordDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = MemberMatchRecord::class;
    }

    /**
     * 记录分页
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function listRecords($startTime, $endTime, $kw, ?int $page, ?int $page_size): array
    {
        $tableName = MemberMatchRecord::getTable();

        $query = MemberMatchRecord::leftJoin('member m', "$tableName.memberId=m.id")
                                  ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
                                  ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
                                  ->search("m.id|m.nickname|m.realname|m.phone", $kw)
                                  ->tableField(true, $tableName)
                                  ->with([
                                      'lawyer' => fn($q) => $q->shortLawyer(),
                                      'consulting' => function ($query) {
                                          $query->with([
                                              'lawyer' => fn($q) => $q->shortLawyer(),
                                          ]);
                                      }
                                  ])
                                  ->order("$tableName.id", 'desc')
                                  ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    public function listByConsultingId($consultingId)
    {
        return MemberMatchRecord::where('consultingId', $consultingId)
                                ->with([
                                    'lawyer' => fn($query) => $query->shortLawyer(),
                                    'consulting'
                                ])
                                ->order("id", 'asc')
                                ->select();

    }
}
